package crossbreed.views;

import java.awt.Color;
import java.awt.Point;
import javax.swing.JScrollPane;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import crossbreed.views.Controls.LineHighlighter;

public class FileView implements IFileView
{
	private JScrollPane scrollPane;
	private JTextComponent textComponent;
	private LineHighlighter highlighter;
	
	public FileView(JTextComponent textComponent, LineHighlighter highlighter, JScrollPane scrollPane)
	{
		this.textComponent = textComponent;
		this.textComponent.setBackground(Color.WHITE);
		this.textComponent.setSelectionColor(new Color(0.5f, 0.5f, 1.0f, 0.5f));
		this.highlighter = highlighter;
		this.scrollPane = scrollPane;
	}

	public void displayDocument(Document doc)
	{
		textComponent.setDocument(doc);
	}

	public void addHighlight(int indexStart, Color color)
	{
		highlighter.addLineHighlighter(indexStart, color);
	}
	
	@Override
	public void scrollToPosition(int position)
	{
		Point currentPosition = scrollPane.getViewport().getViewPosition();
		currentPosition.y = position;
		scrollPane.getViewport().setViewPosition(currentPosition);
	}

	@Override
	public void updateHighlights(int offset, int length)
	{
		highlighter.adjustHighlights(offset, length);
	}
}
